﻿@using System.Text.RegularExpressions;
@using WishlistUtilities;

<script type="text/javascript">
	function deleteItem(itemId) {
		$("#DeleteConfirmationDialog" + itemId).dialog(
			{
				resizable: false, height: 250, width: 300, modal: true, closeOnEscape: true, draggable: false,
				buttons: {
					'Cancel': function() {
						$(this).dialog("close");
					 },
					'OK': function() {
						document.forms['deleteItemForm'+itemId].submit();
					}
				}
			}
		);
	}
    //Show JS Links if user has JS enabled
    $(document).ready(function() {
        $(".scriptsubmit").toggle();
        $(".noscriptsubmit").toggle();
    });
</script>

@{
	PageData["Title"] = "Display Wishlist";
    PageData["displayLinkShare"] = Freemium.IsFeatureEnabledForCurrentUser("Share Lists");

	var username = UrlData[0];
	var listSlug = UrlData[1];

	var db = Database.Open("wishlistdb");

	// lookup the id of the wishlist based on the username and list slug provided
	var wishlistId = ListLookupUtility.LookupListId(username, listSlug);

	if (wishlistId == -1) {
		Response.Redirect("~/Shared/Errors/Error");
	}

	var listToDisplay = db.QuerySingle("SELECT Id FROM List WHERE Id = @0 AND WishlistUserId = @1", 
			wishlistId, WebSecurity.CurrentUserId);

	if (listToDisplay == null) {
		// user doesn't own this list; re-direct to the "Claim Item" view of the page
		Response.Redirect("~/ClaimItem/" + username + "/" + listSlug);
	}

	if (IsPost) {
		if (Request["action"] == "Delete") {
			var itemIdToDelete = Request["itemId"];

			if (!itemIdToDelete.IsInt()) {
				Response.Redirect("~/Shared/Errors/Error");
			}

			// verify user trying to delete this item actually owns the list containing the item
			var itemToDelete = db.QuerySingle("SELECT Item.Id FROM Item, List WHERE Item.ListId = List.Id AND List.WishlistUserId = @0 AND Item.Id = @1",
				WebSecurity.CurrentUserId, itemIdToDelete);

			if (itemToDelete == null) {
				// either not the item owner, or bad item id value
				Response.Redirect("~/Shared/Errors/Error");
			}

			try {
				db.Execute("DELETE FROM Item WHERE Id = @0", itemIdToDelete);
			} catch (Exception) {
				Response.Redirect("~/Shared/Errors/Error");
			}
		}
	}

	var items = db.Query("SELECT * FROM Item WHERE ListId = @0", wishlistId);
	var wishlistName = db.QuerySingle("SELECT Name FROM List WHERE (Id = @0)", wishlistId).Name;

	<h1>@wishlistName</h1>
    
    var sortEnabled = Freemium.IsFeatureEnabledForCurrentUser("Sort Lists");    
	WebGrid grid = new WebGrid(items, rowsPerPage: 5, canSort: sortEnabled);

	@grid.GetHtml(
		columns: grid.Columns(
			grid.Column("Name", "Item"),
			grid.Column("Price", format: @<div>$@item.Price</div>),
			grid.Column("Link", format: @<a href="@item.Link">@item.Link</a>),
			grid.Column(columnName: "Claimed", header: "Item claimed?", format:
				@<text>
					@if (@item.Claimed) {
						@:Yes
					} else {
						@:No
					}
				</text>
			),
			grid.Column("", format:
				@<text>
					<form method="post" action="@Href("EditItem")">
						<input name="itemId" type="hidden" value="@item.Id" />
						<input name="returnUrl" type="hidden" value="@Context.Request.Url" />
						<input type="submit" value="Edit" />
						<input type="button" class="scriptsubmit" value="Delete" onclick="javascript:deleteItem(@(item.Id))" />
					</form>
					<form id="deleteItemForm@(item.Id)" method="post" action="">
                        <input type="submit" class="noscriptsubmit" value="Delete"/>
						<input name="itemId" type="hidden" value="@item.Id" />
						<input name="action" type="hidden" value="Delete" />
					</form>
                    <div id="DeleteConfirmationDialog@(item.Id)" class="jquery-dialog" title="Delete Item">
	                    <span class="ui-icon ui-icon-alert ui-icon-float"></span>
						<p>Are you sure you want to delete @item.Name?</p>
                    </div>
				</text>
			)
		)
	)
	<form method="post" action="@Href("AddItem")">
		<input name="wishlistId" type="hidden" value="@wishlistId" />
		<input name="returnUrl" type="hidden" value="@Context.Request.Url" />					
		<input type="submit" value="Add Item" />
	</form>	
}